<template lang="">
  <UploadExcel :on-success="success" />
</template>
<script>
  import { addEmployeeBatchAPI } from '@/api'
  export default {
    methods: {
      async success ({ header, results }) {
        const userRelations = {
          '入职日期': 'timeOfEntry',
          '手机号': 'mobile',
          '姓名': 'username',
          '转正日期': 'correctionTime',
          '工号': 'workNumber',
          '部门': 'departmentName',
          '聘用形式': 'formOfEmployment'
        }
        const userFrom = results.map(item => {
          const userInfo = {}
          debugger
          Object.keys(item).forEach(obj => {
            if (userRelations[obj] === 'timeOfEntry' || userRelations[obj] === 'correctionTime') {
              item[obj] = this.formatDate(item[obj], '/')
              console.log(item[obj]);
              item[obj] = new Date(item[obj])
            }
            userInfo[userRelations[obj]] = item[obj]
          })
          debugger
          return userInfo
        })
        await addEmployeeBatchAPI(userFrom)
        this.$message.success('导入成功')
        this.$router.back()
      },
      /**
      * 处理 excel 格式的方法
      */
      formatDate (numb, format) {
        const time = new Date((numb - 25567) * 24 * 3600000 - 5 * 60 * 1000 - 43 * 1000 - 24 * 3600000)
        time.setYear(time.getFullYear())
        const year = time.getFullYear() + ''
        const month = time.getMonth() + 1 + ''
        const date = time.getDate() + ''
        if (format && format.length === 1) {
          return year + format + month + format + date
        }
        return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
      }
    },
  }
</script>
<style lang="">

</style>